using System;
using System.Collections;
using System.Collections.Generic;
using BitMango.Diagnostics;
using BMCommon;
using Data;
using Holoville.HOTween;
using TMPro;
using UnityEngine;

public class PackageWidgetHandler : MonoBehaviour, IWidget, IHandlerBase
{
	public void SetData(Package _data)
	{
		Debugger.Assert(_data != null, "PackageWidgetHandler _data != null");
		this.data = _data;
		this.SetPictureLevel();
		this.iapLock.gameObject.SetActive(false);
		this.coinRoot.gameObject.SetActive(false);
		this.progress.Percent = (float)this.data.ClearCount(-1) / (float)this.data.levels.Length * 100f;
		this.icon.gameObject.SetActive(false);
		this.coinCount.text = string.Format("{0}", (this.data.iap != null) ? this.data.iap : string.Empty);
		string text = string.Format("{0}/{1}", this.data.ClearCount(-1), this.data.levels.Length);
		if (this.data.iap != null && this.data.ClearCount(-1) <= 0 && !this.data.record.isLocked)
		{
			this.newIcon.gameObject.SetActive(true);
			this.newIcon.SetSprite("newpackage");
		}
		else if (this.data.iap != null && this.data.record.isLocked && this.data.record.isVideoShowed)
		{
			this.newIcon.gameObject.SetActive(true);
			this.newIcon.SetSprite("freetrial");
		}
		else
		{
			this.newIcon.gameObject.SetActive(false);
		}
		if (this.data.record.isLocked)
		{
			this.iapLock.gameObject.SetActive(true);
			this.coinRoot.gameObject.SetActive(true);
		}
		else if (this.data.record.isClear)
		{
			this.icon.gameObject.SetActive(true);
		}
		else if (this.data.iap != null && !this.data.record.isUnlockAnimation)
		{
			this.iapLock.gameObject.SetActive(true);
			this.coinRoot.gameObject.SetActive(true);
			this.UnLockAnimation();
		}
		this.artRibbon.SetActive(this.data.isArt);
		this.SetStar();
		this.nameText.text = this.data.display;
		this.desc.text = this.data.desc;
		this.clearCount.text = text;
		if (!this.data.record.isLocked)
		{
			this.LoadLevel();
		}
		this.DoDataExchange();
	}

	public void DoDataExchange()
	{
	}

	private void SetStar()
	{
		for (int i = 0; i < this.starWidgets.Count; i++)
		{
			UnityEngine.Object.Destroy(this.starWidgets[i].gameObject);
		}
		this.starWidgets.Clear();
		Vector3 localPosition = new Vector3((float)(this.data.star - 1) * -0.35f, 0f, -1f);
		for (int j = 0; j < this.data.star; j++)
		{
			StarHandler item = Widget.Create<StarHandler>();
			this.starWidgets.Add(item);
			this.starWidgets[j].transform.parent = this.starBg.transform;
			this.starWidgets[j].transform.localPosition = localPosition;
			localPosition.x += 0.7f;
		}
	}

	private void SetPictureLevel()
	{
		for (int i = this.data.levels.Length - 1; i >= 0; i--)
		{
			if (!string.IsNullOrEmpty(this.data.levels[i].pictureTheme))
			{
				this.picturelevel = this.data.levels[i];
				break;
			}
		}
		if (this.picturelevel == null)
		{
			this.picturelevel = this.data.levels[this.data.levels.Length - 1];
		}
	}

	private void LoadLevel()
	{
		this.ClearBoxWidget();
		this.ClearHinderWidget();
		GameMode.gameMode = (GameType)this.picturelevel.mode;
		if (GameMode.gameMode == GameType.TRIANGLE)
		{
			BlocksCoordinate.coordinate = new BlockTriangleCoordinate();
		}
		else if (GameMode.gameMode == GameType.SQUARE)
		{
			BlocksCoordinate.coordinate = new BlockSquareCoordinate();
		}
		else if (GameMode.gameMode == GameType.HEXA)
		{
			BlocksCoordinate.coordinate = new BlockHexaCoordinate();
		}
		else if (GameMode.gameMode == GameType.RHOMBUS)
		{
			BlocksCoordinate.coordinate = new BlockRhombusCoordinate();
		}
		else if (GameMode.gameMode == GameType.TANGRAM)
		{
			BlocksCoordinate.coordinate = new BlockTangramCoordinate();
		}
		BlocksCoordinate.coordinate.coordinate.width = this.picturelevel.width;
		BlocksCoordinate.coordinate.coordinate.height = this.picturelevel.height;
		float num = this.top.transform.position.y - this.bottom.transform.position.y;
		float num2 = 0f;
		if (GameMode.gameMode == GameType.HEXA || GameMode.gameMode == GameType.RHOMBUS)
		{
			num2 = BlocksCoordinate.coordinate.coordinate.SizeY / 2f;
		}
		float num3 = (float)this.picturelevel.height;
		if (GameMode.gameMode == GameType.RHOMBUS)
		{
			num3 *= 0.6666667f;
		}
		BlocksCoordinate.coordinate.coordinate.size = BlocksCoordinate.coordinate.defaultCoordinate.size;
		if (BlocksCoordinate.coordinate.coordinate.SizeY * num3 + num2 > num)
		{
			BlocksCoordinate.coordinate.coordinate.size = num / num3 / (BlocksCoordinate.coordinate.coordinate.SizeY / BlocksCoordinate.coordinate.coordinate.size) - num2 / num3;
		}
		float num4 = (float)this.picturelevel.width;
		if (GameMode.gameMode == GameType.TANGRAM)
		{
			num4 += 2f;
		}
		else if (GameMode.gameMode == GameType.TRIANGLE)
		{
			num4 += 1f;
		}
		else if (GameMode.gameMode == GameType.RHOMBUS)
		{
			num4 *= 0.6666667f;
		}
		float num5 = this.right.transform.localPosition.x - this.left.transform.localPosition.x;
		if (BlocksCoordinate.coordinate.coordinate.SizeX * num4 > num5)
		{
			BlocksCoordinate.coordinate.coordinate.size = num5 / num4 / (BlocksCoordinate.coordinate.coordinate.SizeX / BlocksCoordinate.coordinate.coordinate.size);
		}
		this.GenerateBoard();
		this.GeneratePosition();
	}

	private void GenerateBoard()
	{
		int num = 0;
		for (int i = 0; i < this.picturelevel.blockcount; i++)
		{
			for (int j = 0; j < this.picturelevel.level[i].Length; j++)
			{
				Point point = BlocksCoordinate.coordinate.coordinate.IndexToPuzzle((int)this.picturelevel.level[i][j]);
				bool isUp;
				if (this.picturelevel.isReverse)
				{
					isUp = ((point.x + point.y) % 2 != 0);
				}
				else
				{
					isUp = ((point.x + point.y) % 2 == 0);
				}
				Vector3 localPosition = BlocksCoordinate.coordinate.coordinate.PuzzleToWorld((float)point.x, (float)point.y);
				localPosition.z = 0f;
				PictureBoxWidgetHandler item = Widget.Create<PictureBoxWidgetHandler>();
				this.boxWidgets.Add(item);
				localPosition.z = -2f;
				this.boxWidgets[num].transform.parent = this.boardBg.transform;
				this.boxWidgets[num].transform.localPosition = localPosition;
				this.boxWidgets[num].transform.localScale = Vector3.one * BlocksCoordinate.coordinate.coordinate.size;
				this.boxWidgets[num].tag = "Untagged";
				int index = i;
				if (this.picturelevel.changeColor != null)
				{
					index = (int)this.picturelevel.changeColor[i][j];
				}
				int tangramIndex = (point.x + (int)this.picturelevel.editMinPoint[0] + (((point.y + (int)this.picturelevel.editMinPoint[1]) % 2 != 0) ? 2 : 0)) % 4;
				this.boxWidgets[num].SetData(index, isUp, tangramIndex, this.picturelevel.record.isClear);
				this.boxWidgets[num].name = "box";
				num++;
			}
		}
		if (this.picturelevel.hinder != null)
		{
			for (int k = 0; k < this.picturelevel.hinder.Length; k++)
			{
				Point point2 = BlocksCoordinate.coordinate.coordinate.IndexToPuzzle((int)this.picturelevel.hinder[k]);
				bool isUp2;
				if (this.picturelevel.isReverse)
				{
					isUp2 = ((point2.x + point2.y) % 2 != 0);
				}
				else
				{
					isUp2 = ((point2.x + point2.y) % 2 == 0);
				}
				Vector3 localPosition2 = BlocksCoordinate.coordinate.coordinate.PuzzleToWorld((float)point2.x, (float)point2.y);
				localPosition2.z = 0f;
				HinderWidgetHandler item2 = Widget.Create<HinderWidgetHandler>();
				this.hinderWidgets.Add(item2);
				this.hinderWidgets[k].transform.parent = this.boardBg.transform;
				this.hinderWidgets[k].transform.localPosition = localPosition2;
				this.hinderWidgets[k].transform.localScale = Vector3.one * BlocksCoordinate.coordinate.coordinate.size;
				this.hinderWidgets[k].name = "hinder";
				int tangramIndex2 = (point2.x + (int)this.picturelevel.editMinPoint[0] + (((point2.y + (int)this.picturelevel.editMinPoint[1]) % 2 != 0) ? 2 : 0)) % 4;
				this.hinderWidgets[k].SetData(isUp2, tangramIndex2);
			}
		}
	}

	private void GeneratePosition()
	{
		float num = 9999f;
		float num2 = -9999f;
		for (int i = 0; i < this.boxWidgets.Count; i++)
		{
			if (this.boxWidgets[i].transform.position.y < num)
			{
				num = this.boxWidgets[i].transform.position.y;
			}
			if (this.boxWidgets[i].transform.position.y > num2)
			{
				num2 = this.boxWidgets[i].transform.position.y;
			}
		}
		for (int j = 0; j < this.hinderWidgets.Count; j++)
		{
			if (this.hinderWidgets[j].transform.position.y < num)
			{
				num = this.hinderWidgets[j].transform.position.y;
			}
			if (this.hinderWidgets[j].transform.position.y > num2)
			{
				num2 = this.hinderWidgets[j].transform.position.y;
			}
		}
		Vector3 position = base.transform.position;
		float num3 = (num - position.y + (num2 - position.y)) / 2f;
		this.boardBg.transform.localPosition = new Vector3(this.boardBg.transform.localPosition.x, this.boardBg.transform.localPosition.y - num3, this.boardBg.transform.localPosition.z);
	}

	private void ClearBoxWidget()
	{
		for (int i = 0; i < this.boxWidgets.Count; i++)
		{
			UnityEngine.Object.Destroy(this.boxWidgets[i].gameObject);
		}
		this.boxWidgets.Clear();
	}

	private void ClearHinderWidget()
	{
		for (int i = 0; i < this.hinderWidgets.Count; i++)
		{
			UnityEngine.Object.Destroy(this.hinderWidgets[i].gameObject);
		}
		this.hinderWidgets.Clear();
	}

	public void OnClick_Self(tk2dUIItem obj)
	{
		BMSoundHelper.Play("se_btnclick", false, 1f);
		HOTween.Complete(base.name + string.Empty + this.data.name);
		this.parent = Scene.FindSceneRoot(obj.transform.parent.gameObject);
		this.parent.SendMessage("OnClick_package", this);
	}

	private void UnLockAnimation()
	{
		if (this.data.iap != null && !this.data.record.isUnlockAnimation)
		{
			base.StartCoroutine(this.coUnlockPopupAnimation());
			this.data.record.isUnlockAnimation = true;
			this.data.SaveContext();
		}
	}

	private IEnumerator coUnlockPopupAnimation()
	{
		this.boardBg.transform.localScale = Vector3.zero;
		this.iapLock.gameObject.ShakeRot(1f, 7f);
		yield return new WaitForSeconds(0.3f);
		HOTween.To(this.iapLock.transform, 0.3f, new TweenParms().Prop("localScale", Vector3.zero).Ease(EaseType.EaseInBack));
		HOTween.To(this.clearbg.transform, 0.3f, new TweenParms().Prop("localScale", Vector3.zero).Ease(EaseType.EaseInBack));
		HOTween.To(this.coinRoot.transform, 0.3f, new TweenParms().Prop("localScale", Vector3.zero).Ease(EaseType.EaseInBack));
		yield return new WaitForSeconds(0.3f);
		this.iapLock.gameObject.SetActive(false);
		this.coinRoot.gameObject.SetActive(false);
		this.boardBg.transform.localScale = Vector3.zero;
		this.crossParticle.Play();
		HOTween.To(this.boardBg.gameObject.transform, 0.3f, new TweenParms().Prop("localScale", Vector3.one).Ease(EaseType.EaseOutBack));
		HOTween.To(this.clearbg.gameObject.transform, 0.3f, new TweenParms().Prop("localScale", Vector3.one).Ease(EaseType.EaseOutBack));
		BMSoundHelper.Play("se_hintin", false, 1f);
		yield break;
	}

	private GameObject parent;

	public Package data;

	public TextMeshPro nameText;

	public TextMeshPro desc;

	public TextMeshPro clearCount;

	public TextMeshPro coinCount;

	public GameObject clearbg;

	public tk2dSprite icon;

	public tk2dSprite iapLock;

	public tk2dProgress progress;

	public ParticleSystem crossParticle;

	public tk2dSprite newIcon;

	public GameObject starBg;

	public GameObject coinRoot;

	public GameObject top;

	public GameObject bottom;

	public GameObject left;

	public GameObject right;

	public GameObject boardBg;

	public GameObject artRibbon;

	private List<HinderWidgetHandler> hinderWidgets = new List<HinderWidgetHandler>();

	private List<PictureBoxWidgetHandler> boxWidgets = new List<PictureBoxWidgetHandler>();

	private List<StarHandler> starWidgets = new List<StarHandler>();

	private Level picturelevel;
}
